
const app = getApp()
Page({
  data: {
    showChoosePrice: false,
    columns: ['余额支付(免密)', '微信支付'],
    active: 0,
    mallId: 0,
    pay: 0,
    dellId: 0,
    mall: {},
    sell: 0,
    exchange: 0,
    pageNum: 1,
    pageTotal: 0,
    user_list: []
  },
  onLoad: function (options) {
    this.setData({
      mallId: options.id,
      user_list: []
    })
    this.getUserList()
    this.getMallDetail()
    wx.showShareMenu({
      withShareTicket: true,
    })
  },
  onShareTimeline() {
    return {
      title: '梦与玩',
      query: `/pages/goods/goods-detail/goods-detail?id=${this.data.mallId}`
    }
  },
  onShareAppMessage() {
    return {
      title: '梦与玩',
      query: `/pages/goods/goods-detail/goods-detail?id=${this.data.mallId}`
    }
  },
  getMallDetail() {
    let _this = this
    app.util.request({
      url: 'api/mall/findById',
      method: 'get',
      data: {
        id: _this.data.mallId
      },
      success(response) {
        if (response.data.code == 0) {
          _this.setData({
            mall: response.data.data
          })
        }
      }
    })
  },
  getUserList() {
    let _this = this
    app.util.request({
      url: 'api/deal/limit',
      method: "post",
      data: {
        awardMallId: _this.data.mallId,
        state: _this.data.active,
        page: _this.data.pageNum,
        pageSize: 10,
      },
      success(response) {
        if (response.data.code == 0) {
          _this.setData({
            pageTotal: response.data.data[0].deal.pages,
            exchange: response.data.data[0].exchange,
            sell: response.data.data[0].sell,
            user_list: [..._this.data.user_list, ...response.data.data[0].deal.list]
          })
        }
      }
    })
  },

  toPersonalDetail(e) {
    if (this.data.active == 0) {
      wx.navigateTo({
        url: '/pages/market/market-personal-goods/market-personal-goods?id=' + e.currentTarget.dataset.id + '&dealId=' + this.data.mallId,
      })
    } else {
      if (this.data.dellId === e.currentTarget.dataset.id) {
        this.setData({
          dellId: -1
        })
      } else {
        this.setData({
          type: e.currentTarget.dataset.state,
          dellId: e.currentTarget.dataset.id,
        })
      }

    }
  },
  showChoosePay() {
    if (this.data.dellId == 0) {
      wx.showToast({
        icon: 'none',
        title: '请选择交易用户',
      })
    } else if (this.data.type == 1) {
      this.setData({
        showChoosePrice: true
      })
    } else {
      this.marketBuy()
    }
  },
  marketBuy() {
    let _this = this
    let query = { dealId: _this.data.dellId }
    if (_this.data.type == 1)
      query["pay"] = _this.data.pay
    let url = _this.data.type == 1 ? 'api/orders/deal' : 'api/orders/sale'
    app.util.request({
      url: url,
      method: "post",
      data: query,
      success(response) {
        if (query.pay == 1) {
          _this.weixinPay(response.data.data)
        } else if (response.data.code == 0) {
          wx.showModal({
            title: '提示',
            content: '交易成功',
            cancelText: '留在本页',
            confirmText: '前往查看',
            success(res) {
              if (res.confirm) {
                wx.navigateTo({
                  url: '/pages/goods/my-goods-index',
                })
              } else if (res.cancel) {
                _this.setData({
                  dellId: -1,
                  active: 0,
                  showChoosePrice: false,
                })
                _this.getUserList()
              }
            }
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: response.data.msg,
          })
        }
      }
    })
  },
  weixinPay(tradeNo) {
    let _this = this
    app.util.request({
      url: 'wxPay/miniAppPay',
      method: "post",
      data: {
        outTradeNo: tradeNo
      },
      success(response) {
        if (response.data.code == 0) {
          let data = response.data.data;
          wx.requestPayment({
            timeStamp: data.timeStamp,
            nonceStr: data.nonceStr,
            package: data.package,
            signType: data.signType,
            paySign: data.paySign,
            appId: data.appId,
            success(res) {
              if (res.errMsg == "requestPayment:ok") {
                wx.showToast({
                  title: '支付成功！',
                })
                _this.setData({
                  hasPay: true
                })
                setTimeout(() => {
                  wx.navigateTo({
                    url: '/pages/goods/my-goods-index',
                  })
                }, 1500)
              }
            },
            fail(res) {
              console.log(res)
              _this.setData({
                hasPay: false
              })
            }
          })

        }
      }
    })
  },
  onChange(event) {
    this.setData({
      active: event.detail.name,
      pageNum: 1,
      user_list: []
    })
    this.getUserList()
  },
  onClose() {
    this.setData({
      dellId:-1,
      showChoosePrice: false
    })
  },
  onConfirm(e) {
    this.setData({
      pay: e.detail.index
    })
  },
  begForBuy() {
    wx.navigateTo({
      url: '/pages/want-get/want-get?id=' + this.data.mallId,
    })
  },
})
